﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;

namespace Hidistro.UI.Web.API
{
    public class Log
    {
        private string savePath = HttpContext.Current.Server.MapPath("~") + "/log/";
        private string saveLog = string.Empty;
        public Log(string path)
        {
            if (path.Last() != '/') path += "/";
            this.savePath = HttpContext.Current.Server.MapPath("~") + path;
        }
        public void addLine(string log)
        {
            this.saveLog += log + "\r\n";
        }
        public void logSave()
        {
            StreamWriter Stream = this.init();
            Stream.WriteLine("==" + DateTime.Now.ToShortTimeString() + "==============================\r\n");
            Stream.Write(this.saveLog + "\r\n");
            Stream.Flush();
            Stream.Close();
            this.saveLog = string.Empty;
        }
        private StreamWriter init()
        {
            if (!Directory.Exists(this.savePath))
            {
                Directory.CreateDirectory(this.savePath);
            }
            string nowData = DateTime.Now.ToShortDateString();
            nowData = nowData.Replace("/", "-");
            string nowLogName = nowData + ".log";
            if (File.Exists(this.savePath + nowLogName))
            {
                return File.AppendText(this.savePath + nowLogName);
            }
            else
            {
                return File.CreateText(this.savePath + nowLogName);
            }
        }
    }
}
